package com.jiejie.controller;


import com.jiejie.entity.Coupon;
import com.jiejie.entity.LoginCustomer;
import com.jiejie.service.ICouponService;
import com.jiejie.service.ICustomerAndCouponService;
import com.jiejie.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 辰广
 * @since 2022-10-05
 */
@RestController
@RequestMapping
public class CouponController {
    @Autowired
    ICouponService couponService;
    @Autowired
    ICustomerAndCouponService customerAndCouponService;
    @GetMapping("receiveCoupon")
    public String receive(@RequestParam("couId") Integer couId, HttpServletRequest request){
        String token=request.getHeader("token");
        System.out.println("token:"+token);
        LoginCustomer loginCustomer =null;
        if (!token.equals("null")) {
            //1.登陆用户数据
            loginCustomer = TokenUtil.parseToken(token);
        }
        System.out.println("============================");
        System.out.println(couId);
        System.out.println(loginCustomer);
        Integer customerId=loginCustomer.getCustomerId();
        Coupon coupon=couponService.receiveCoupon(customerId,couId);
        System.out.println(coupon);
        if(coupon!=null){
            customerAndCouponService.receiveCouForCustomer(couId,customerId);
            System.out.println("===========领取成功============");
        }
        return "ok";
    }
}
